package com.lc.demo.service.impl;

import com.lc.demo.bean.Score;
import com.lc.demo.bean.Student;
import com.lc.demo.mapper.StudentMapper;
import com.lc.demo.service.ClassGradleService;
import com.lc.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName StudentServiceImpl
 * @Deacription TODO
 * @Author jinshuhan
 * @Version 1.0
 **/
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student login(String username, String password) {
        return studentMapper.selectStudentByUsernameAndPaassword(username, password);
    }

    @Override
    public List<Score> getScoreListByUsername(String username) {
        return studentMapper.getScoreListByUsername(username);
    }

    @Override
    public Student selectByUsername(String username) {
        return studentMapper.selectByUsername(username);
    }

    @Autowired
    ClassGradleService classGradleService;

    @Override
    public void addStudent(Student student) {
        String className = classGradleService.getClassNameById(student.getClassid());
        student.setClassname(className);
        studentMapper.addStudent(student);
    }

    @Override
    public void delStudentByUsername(String username) {
        studentMapper.delStudentByUsername(username);
    }

    @Override
    public List<Student> getStudentList() {
        return studentMapper.getStudentList();
    }

    @Override
    public void updateStudent(Student student) {
        String classNameById = classGradleService.getClassNameById(student.getClassid());
        student.setClassname(classNameById);
        studentMapper.updateStudent(student);
    }

    @Override
    public Student analyse(Student student) {

        Student student1 = studentMapper.selectByUsername(student.getUsername());

        //先查询出  班级排名
        List<Student> studentClassrankList = studentMapper.classrank(student1);
        for (int i = 0; i < studentClassrankList.size(); i++) {
            //判断是否是当前用户
            if (studentClassrankList.get(i).getUsername().equals(student1.getUsername())) {
                student1.setClassrank(i + 1);
                student1.setAvgscore(studentClassrankList.get(i).getAvgscore());
            }
        }


        //在查询出  专业排名
        List<Student> studentGraderankList = studentMapper.graderank(student1);
        for (int i = 0; i < studentGraderankList.size(); i++) {
            //判断是否是当前用户
            if (studentGraderankList.get(i).getUsername().equals(student1.getUsername())) {
                student1.setGraderank(i + 1);
            }
        }

        return student1;
    }



   /* @Autowired
    private StudentMapper studentMapper;
    @Override
    public Student login(String stuId, String stuPass)
    {
        return studentMapper.selectStudentByIdAndPass(stuId,stuPass);
    }

    @Override
    public List<Student> getAllStudent() {
        return studentMapper.selectAllStudent();
    }

    @Override
    public int addStudent(Student student) {
        return studentMapper.insertStudent(student);
    }

    @Override
    public Student selectById(String stuId) {
        return studentMapper.selectStudentByStuId(stuId);
    }

    @Override
    public int deleStu(String stuId) {
        return studentMapper.deleteStuById(stuId);
    }

    @Override
    public List<Student> seleStuByClassName(String stuClass) {
        return studentMapper.selectStuByStuclass(stuClass);
    }

    @Override
    public int addStudentHavePass(Student student) {
        return studentMapper.insertStudentHavaPass(student);
    }*/
}
